package com.zhuoyue.biz.core.command.impl.panel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhuoyue.biz.core.command.CmdParams;
import com.zhuoyue.biz.core.command.model.PanelCmd;
import com.zhuoyue.biz.core.command.model.RadarConfigurationCmd;
import com.zhuoyue.common.utils.CmdUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * 四路联控面板
 *
 * @author wjming
 */
public class FourWayJointControlPanel0Fab0602 {
    private static String SET_CMD = "0201${gatewayMac}${deviceMac}03EF1401${nightLightTime}";
    private static String TIME_00 = "00";
    private static String TIME_FF = "FF";
    private static int TIME_10 = 10;
    private static int TIME_60 = 60;
    /**
     * 小夜灯亮的时间
     */
    private static final String NIGHT_LIGHT_TIME = "nightLightTime";
    private static final String ACTIONS = "actions";
    private static final String GATEWAY_MAC = "gatewayMac";
    private static final String DEVICE_MAC = "deviceMac";
    private static final String WAY_NUMBER = "wayNumber";
    private static final String CONTROL_STATE = "controlState";
    private static final String CONTROL_TYPE = "controlType";

    public static String getCommand(String controlParam) {
        Map<String, String> map = (Map) JSONObject.parse(controlParam);
        String controlType = map.get(CONTROL_TYPE);
        String cmd = "";

        if (StringUtils.isEmpty(controlType)) {
            Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get(ACTIONS)));
            if (StringUtils.isNotEmpty(actionsMap.get(NIGHT_LIGHT_TIME))) {
                cmd = getRadarontrolCmd(map);
                return cmd;
            } else {

            }
            return null;
        }


        if (CmdParams.ControlType.CONTROL_TYPE_DIRECT.equals(controlType)) {
            cmd = getDirectControlCmd(map);
        }
        return cmd;
    }


    private static String getDirectControlCmd(Map<String, String> map) {
        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get(ACTIONS)));
        String controlState = CmdUtils.intToHexTwo(Integer.parseInt(actionsMap.get(CONTROL_STATE)));
        String wayNumber = actionsMap.get(WAY_NUMBER);
        switch (wayNumber) {
            case CmdParams.Panel.WAY_1:
                return new PanelCmd(map.get(GATEWAY_MAC), map.get(DEVICE_MAC), CmdParams.Od0Fab.WAY_1, controlState).getCmd(CmdParams.CmdModel.PANEL_0FAB);
            case CmdParams.Panel.WAY_2:
                return new PanelCmd(map.get(GATEWAY_MAC), map.get(DEVICE_MAC), CmdParams.Od0Fab.WAY_2, controlState).getCmd(CmdParams.CmdModel.PANEL_0FAB);
            case CmdParams.Panel.WAY_3:
                return new PanelCmd(map.get(GATEWAY_MAC), map.get(DEVICE_MAC), CmdParams.Od0Fab.WAY_3, controlState).getCmd(CmdParams.CmdModel.PANEL_0FAB);
            case CmdParams.Panel.WAY_4:
                return new PanelCmd(map.get(GATEWAY_MAC), map.get(DEVICE_MAC), CmdParams.Od0Fab.WAY_4, controlState).getCmd(CmdParams.CmdModel.PANEL_0FAB);
            default:
                return null;
        }
    }


    private static String getRadarontrolCmd(Map<String, String> map) {
        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get(ACTIONS)));
        String nightLightTime = actionsMap.get(NIGHT_LIGHT_TIME);
        if (TIME_FF.equals(nightLightTime) || TIME_00.equals(nightLightTime)) {
            return new RadarConfigurationCmd(map.get(GATEWAY_MAC), map.get(DEVICE_MAC), nightLightTime).getCmd(SET_CMD);
        }
        if (Integer.parseInt(nightLightTime) >= TIME_10 && Integer.parseInt(nightLightTime) <= TIME_60) {
            nightLightTime = CmdUtils.intToHexTwo(Integer.parseInt(nightLightTime));
            return new RadarConfigurationCmd(map.get(GATEWAY_MAC), map.get(DEVICE_MAC), nightLightTime).getCmd(SET_CMD);
        } else {
            return null;
        }
    }

}
